package controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

import patronBuilder.ControladorVentanaAtributo;

import modelo.Ingrediente;
import modelo.IngredienteDAO;
import Vista.NewJDialogIngrediente;

public class ControladorIngrediente extends ControladorVentanaAtributo{
	private NewJDialogIngrediente ingred;
	
	public ControladorIngrediente() {
		super();
	}
	
	@Override
	public void mostrar() {
		ingred = new NewJDialogIngrediente (null, this);
		
		ingred.getjBtoAgregar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				IngredienteDAO indao = new IngredienteDAO();
				indao.registrarIngrediente(((new Ingrediente(ingred.getjTxtCodigoIngrediente().getText(),ingred.getjTxtNombreIngrediente().getText(),Double.parseDouble(ingred.getjTxtStockMinimo().getText()),Double.parseDouble(ingred.getjTxtStockMaximo().getText()),Integer.parseInt(ingred.getjTxtCantidad().getText())))));
				ingred.dispose();
				JOptionPane.showMessageDialog(null, " Ingrediente registrado", "Informacion", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		ingred.setVisible(true);
		ingred.getjBtoSalir().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ingred.dispose();
			}
		});
		this.ingred.setVisible(true);
		
		ingred.getjBtoIngredBuscar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				IngredienteDAO dao = new IngredienteDAO();		
				
				Ingrediente in = dao.ConsultarIngrediente(ingred.getjTxtCodigoIngrediente().getText()); 
				if (in == null){
					JOptionPane.showMessageDialog(null, "No existe.Puede continuar");
					ingred.getjBtoAgregar().setEnabled(true);
				}
				else
					JOptionPane.showMessageDialog(null, "Producto existente, inserte otro codigo.");	
			}
		});
	}
	
	
}

